psrc_census.R functions

These are instructions on how to use the functions in the libary psrc_census.R

The code requires several libraries. The config file contains global values specific to PSRC. The main code is psrc_census.R.

Currently there are two functions: * psrc_acs_table retrieves acs data for a variable and puts it in a table at a chosen geography, for a chosen year * create_tract_map makes a map of the region at the tract level for a variable

library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.0.5
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.3     v purrr   0.3.4
## v tibble  3.1.1     v dplyr   1.0.5
## v tidyr   1.1.3     v stringr 1.4.0
## v readr   1.4.0     v forcats 0.5.1
## Warning: package 'tibble' was built under R version 4.0.5
## Warning: package 'tidyr' was built under R version 4.0.5
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(sf)
## Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1
library(leaflet)
library(tidycensus)
## Warning: package 'tidycensus' was built under R version 4.0.5
library(writexl)
## Warning: package 'writexl' was built under R version 4.0.5
source('../library/psrc_census_config.R')
source('../library/psrc_census.R')

Set up your API key

The first time you run this code, you will need to set our Census API Key as an environment variable, if you haven’t doen taht. After that you can just get it. This is the website to get a key: https://api.census.gov/data/key_signup.html. Once you run Sys.setenv on the Census API Key you will only need to run Sys.getenv.

#Sys.setenv(CENSUS_API_KEY = 'PUT YOUR KEY HERE')
Sys.getenv("CENSUS_API_KEY")

Next you need to decide what tables you would like to download. This is the hardest part because you have find the correct table code, decide on geography, and which years. The main function psrc_acs_table has four parameters: tbl_code, geog, yr,acs.

  • tbl_code is the code of the ACS table list on the Census website, in string format, such as “B02001_005”
  • geog is the geography, currently with the options of “county” or “tract”
  • yr is the numeric year of the data, such as 2019
  • acs is which dataset you are using, such as “acs1” or “acs5”

You can find the list of ACS datasets available via the api here: https://www.census.gov/data/developers/data-sets.html

Get the data —————————————————–

#psrc_table(tbl_code, geog, yr,acs)

tract_asian<-psrc_acs_table("B02001_005", "tract", 2019,'acs5')
## Getting data from the 2015-2019 5-year ACS
## # A tibble: 776 x 8
##    GEOID   NAME          variable estimate   moe ACS_Year ACS_Type ACS_Geography
##    <chr>   <chr>         <chr>       <dbl> <dbl>    <dbl> <chr>    <chr>        
##  1 530330~ Census Tract~ B02001_~     1202   248     2019 acs5     tract        
##  2 530330~ Census Tract~ B02001_~     1168   232     2019 acs5     tract        
##  3 530330~ Census Tract~ B02001_~      484   155     2019 acs5     tract        
##  4 530330~ Census Tract~ B02001_~      970   418     2019 acs5     tract        
##  5 530330~ Census Tract~ B02001_~      465   194     2019 acs5     tract        
##  6 530330~ Census Tract~ B02001_~      278   143     2019 acs5     tract        
##  7 530330~ Census Tract~ B02001_~     1094   355     2019 acs5     tract        
##  8 530330~ Census Tract~ B02001_~      997   254     2019 acs5     tract        
##  9 530330~ Census Tract~ B02001_~      412   195     2019 acs5     tract        
## 10 530330~ Census Tract~ B02001_~      323    64     2019 acs5     tract        
## # ... with 766 more rows
county_ferry<-psrc_acs_table("B08006_013", "county", 2019, 'acs1')
## The 1-year ACS provides data for geographies with populations of 65,000 and greater.
## Getting data from the 2019 1-year ACS
## # A tibble: 4 x 8
##   GEOID NAME            variable  estimate   moe ACS_Year ACS_Type ACS_Geography
##   <chr> <chr>           <chr>        <dbl> <dbl>    <dbl> <chr>    <chr>        
## 1 53033 King County     B08006_0~     1687   854     2019 acs1     county       
## 2 53035 Kitsap County   B08006_0~     9788  2076     2019 acs1     county       
## 3 53053 Pierce County   B08006_0~      118   124     2019 acs1     county       
## 4 53061 Snohomish Coun~ B08006_0~       61   106     2019 acs1     county
region_brazilian<-psrc_acs_table("B04006_22", "region", 2019, 'acs1')
## The 1-year ACS provides data for geographies with populations of 65,000 and greater.
## Getting data from the 2019 1-year ACS
## Warning: You have not specified the estimates associated with the margins of
## error. In the event that your calculation involves multiple zero estimates, this
## will unnaturally inflate the derived margin of error.
## # A tibble: 0 x 7
## # ... with 7 variables: variable <chr>, estimate <dbl>, moe <dbl>,
## #   ACS_Year <dbl>, ACS_Type <chr>, total_region <dbl>, moe_region <dbl>
#if you want you can write the data to the clipboard or out to csv or excel
write.table(county_ferry, "clipboard", sep="\t", row.names=FALSE)
write.csv(county_ferry, "ferry_workers_by_county.csv")
write_xlsx(county_ferry, "ferry_workers_by_county.xlsx")

Map the data —————————————————–

create_tract_map, we send in a data frame by tract, and it makes a map.

create_tract_map(tract_asian)
## Reading layer `dbo.tract2010_nowater' from data source `MSSQL:server=AWS-PROD-SQL\Sockeye;database=ElmerGeo;trusted_connection=yes' using driver `MSSQLSpatial'
## Simple feature collection with 773 features and 19 fields
## geometry type:  MULTIPOLYGON
## dimension:      XY
## bbox:           xmin: 1099353 ymin: -97548.53 xmax: 1622631 ymax: 477101.5
## projected CRS:  NAD83 / Washington North (ftUS)